﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZFine.Application.Project;
using ZFine.Code;
using ZFine.Web;

namespace Logiv.WEB.Areas.Project.Controllers
{
    public class ExecuteWorkPlanController : BaseController
    {
        private OperationGudaoCarApp servier = new OperationGudaoCarApp();
        private PositionRecordApp Serverpostion = new PositionRecordApp();
        public override ActionResult Index() 
        {
            return View();
        }

        public override ActionResult Details()
        {
            var keyValue = Request.QueryString["keyValue"];
            var thisoperationgudao = servier.GetEntityIncluedTwo(f => f.F_Id == keyValue);
            if (thisoperationgudao != null && thisoperationgudao.WorkPlan != null)
            {
                List<ClientPlay> jihualist =
                    ZFine.Code.Json.ToList<ClientPlay>(thisoperationgudao.WorkPlan.BianZhiPlan);
                if (jihualist != null && jihualist.Count > 0)
                {
                    ViewData["jihualist"] = jihualist;
                }
                else
                {
                    ViewData["jihualist"] = new List<ClientPlay>();
                }
                ViewData["operationent"] = thisoperationgudao;
            }
            else
            {
                ViewData["operationent"] = null;
            }

            return View();
        }
        /// <summary>
        /// 执行计划
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ExecutePlan(string keyValue)
        {
            string msg = string.Empty;
            int code = 0;
            var thisoperationgudao = servier.GetEntityInclued(f => f.F_Id == keyValue);
            if (thisoperationgudao != null && thisoperationgudao.WorkPlan != null)
            {
                if (thisoperationgudao.IsExecute!=true)
                {
                    var notWorkplan = servier.GetOnTrackEnt(f =>
                        f.State == true && f.F_CreatorTime < thisoperationgudao.F_CreatorTime && f.IsExecute == false);
                    if (notWorkplan == null)
                    {
                        code = servier.SubmitExecutePlan(thisoperationgudao, ref msg);
                    }
                    else
                    {
                        code = -1;
                        msg = "请按照创建计划的时间顺序执行计划！";
                    }
                }
                else
                {
                    code = -1;
                    msg = "已经执行了的计划请不要重复执行";
                }
            }
            else
            {
                code = -1;
                msg = "传入数据为空,或者无计划";
            }

            //if (code >= 0)
            //{
            //    return Success(msg);
            //}
            //else
            //{
            //    return Error(msg);
            //}
            return Json(new { msg = msg, code = code }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 回退计划
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ExecuteReturnPlan(string keyValue)
        {
            string msg = string.Empty;
            int code = 0;
            var thisoperationgudao = servier.GetEntityInclued(f => f.F_Id == keyValue);
            if (thisoperationgudao != null && thisoperationgudao.WorkPlan != null)
            {
                if (thisoperationgudao.IsExecute == true)
                {
                    if (thisoperationgudao.IsRollBack != true)
                    {
                        var banci = Serverpostion.GetForm(f => f.State == true);
                        if (banci.F_Id != thisoperationgudao.Banci)
                        {
                            code = -1;
                            msg = "班次不同无法回退执行";
                        }
                        else {
                            var executeworkplan = servier.GetOnTrackEnt(f =>
                          f.IsExecute == true && f.ExecuteDateTime > thisoperationgudao.ExecuteDateTime);
                            if (executeworkplan == null)
                            {
                                code = servier.SubmitExecuteReturnPlan(thisoperationgudao, ref msg);
                            }
                            else
                            {
                                code = -1;
                                msg = "此计划不是最新执行的计划，无法回退";
                            }
                        }
                      
                    }
                    else
                    {
                        code = -1;
                        msg = "此计划已经完成了回退，请不要重复执行";
                    }

                }
                else
                {
                    code = -1;
                    msg = "此计划还没有执行，无法回退";
                }
            }
            else
            {
                code = -1;
                msg = "传入数据为空,或者无计划";
            }

            return Json(new { msg = msg, code = code }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取计划列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonAuto(Pagination pagination)
        {
            try
            {
                var reuslt = servier.GetListD(pagination,f => f.State == true&&f.OperationType == 2);//获取当前有效的所有股道上的车
                var data = new
                {
                    rows = reuslt,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }


        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult DeleteForm(string keyValue)
        {
            int code=servier.modifDeletePlan(keyValue);
            if (code >= 0)
            {
                return Success("删除成功。");
            }
            else
            {
                return Error("删除失败，请稍后重试");
            }
        }
    }
}